<template>
    <el-dropdown trigger="click" @command="handleSetLanguage">
        <el-button size="mini" type="text">
            <i class="iconfont">&#xe635;</i>{{language==='en' ? "English" : "中文"}} <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
            <el-dropdown-item style="min-width: 0;" command="zh" :disabled="language==='zh'">中文</el-dropdown-item>
            <el-dropdown-item style="min-width: 0;" command="en" :disabled="language==='en'">English</el-dropdown-item>
        </el-dropdown-menu>
    </el-dropdown>
</template>

<script>
    import {getLangLocale} from "../../utils/i18n";

    export default {
        computed: {
            language() {
                console.log("language : " + this.$store.getters.language);
                return this.$store.getters.language;
            }
        },
        methods: {
            //切换语言
            handleSetLanguage(language) {
                // console.log("handleSetLanguage - language --> " + language);
                this.$i18n.locale = language;
                this.$store.dispatch('setLanguage', language);

                //请求common多语言数据
                getLangLocale(language, "common", "", () => {
                    //请求page的多语言数据
                    getLangLocale(language, "pages", this.$router.currentRoute.name);
                });
            }
        }
    }
</script>
